package com.huifer.wgx.service.impl;

import com.huifer.wgx.model.InteriorPointEntity;
import com.huifer.wgx.model.ResultPointEntity;
import com.huifer.wgx.repository.InteriorPointRepository;
import com.huifer.wgx.repository.ResultPointRepository;
import com.huifer.wgx.service.ResultPointService;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.omg.CORBA.INTERNAL;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * <p>Title : ResultPointServiceImpl </p>
 * <p>Description : </p>
 *
 * @author huifer
 * @date 2019-03-22
 */
@Service
public class ResultPointServiceImpl implements ResultPointService {

    @Autowired
    private ResultPointRepository resultPointRepository;
    @Autowired
    private InteriorPointRepository iPointRepository;

    /**
     * 将内业点转换成成果点
     *
     * @param workId 工作id
     * @param personnelId 内业人员id
     * @return 成果点
     */
    @Override
    public List<ResultPointEntity> inter2ResultPoint(Long workId, Long personnelId) {
        try {
            Specification<InteriorPointEntity> spec = (Specification<InteriorPointEntity>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                    .and(
                            criteriaBuilder.equal(root.get("workId"), workId),
                            criteriaBuilder.equal(root.get("personnelId"), personnelId)
                    );
            List<InteriorPointEntity> inPoints = iPointRepository.findAll(spec);
            List<ResultPointEntity> saveList = calcResultPoint(inPoints, workId, personnelId);
            return resultPointRepository.saveAll(saveList);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取所有校验通过的点，并且制作成成果点
     *
     * @param inPoints 所有点
     * @param workId 工作id
     * @param personnelId 内业人员id
     * @return 成果点
     */
    private List<ResultPointEntity> calcResultPoint(List<InteriorPointEntity> inPoints, Long workId,
            Long personnelId) {
        try {
            // 需要和校验表关联比对 过滤ipoints
            List<ResultPointEntity> result = new ArrayList<>();
            for (int i = 0; i < inPoints.size(); i++) {
                ResultPointEntity resultPointEntity = new ResultPointEntity();
                InteriorPointEntity interPoint = inPoints.get(i);

                resultPointEntity.setResultPointName(interPoint.getInteriorPointName());
                resultPointEntity.setResultPointX(interPoint.getInteriorPointX());
                resultPointEntity.setResultPointY(interPoint.getInteriorPointY());
                resultPointEntity.setResultPointZ(interPoint.getInteriorPointZ());
                resultPointEntity.setResultPointDeep(interPoint.getInteriorPointDeep());
                resultPointEntity.setResultPointIsmesh(interPoint.getInteriorPointIsmesh());
                resultPointEntity.setResultPointSize(interPoint.getInteriorPointSize());
                resultPointEntity
                        .setResultPointManholeCoverType(
                                interPoint.getInteriorPointManholeCoverType());
                resultPointEntity.setPointType(interPoint.getPointType());

                // 关联关系
                resultPointEntity.setWorkId(workId);
                resultPointEntity.setPersonnelId(personnelId);

                result.add(resultPointEntity);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存成果点
     *
     * @param iPoint 成果点
     * @return 成果点
     */
    @Override
    public ResultPointEntity saveOne(ResultPointEntity iPoint) {
        try {
            return resultPointRepository.save(iPoint);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 跟新成果点
     *
     * @param iPoint 成果点
     * @return 成果点
     */
    @Override
    public ResultPointEntity updateIpoint(ResultPointEntity iPoint) {
        try {
            return resultPointRepository.save(iPoint);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询所有
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 成果点
     */
    @Override
    public Page<ResultPointEntity> findAll(Integer pageNum, Integer pageSize) {
        try {
            return getResultPointEntity(pageNum, pageSize, null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据工程id查询所有
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @param workId 工程id
     * @return 成果点结果
     */
    @Override
    public Page<ResultPointEntity> findByWorkId(Integer pageNum, Integer pageSize, Long workId) {
        try {
            Specification<ResultPointEntity> spec = (Specification<ResultPointEntity>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                    .equal(root.get("workId"), workId);
            return getResultPointEntity(pageNum, pageSize, spec);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据内业人员id查询所有
     *
     * @param personnelId 内业id
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 成果点结果
     */
    @Override
    public Page<ResultPointEntity> findByPersonnelId(Long personnelId, Integer pageNum,
            Integer pageSize) {
        try {
            Specification<ResultPointEntity> spec = (Specification<ResultPointEntity>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder
                    .equal(root.get("personnelId"), personnelId);
            return getResultPointEntity(pageNum, pageSize, spec);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取全部成果点
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @param spec 条件
     * @return 成果点
     */
    private Page<ResultPointEntity> getResultPointEntity(Integer pageNum, Integer pageSize,
            Specification<ResultPointEntity> spec) {
        Sort sort = new Sort(Direction.DESC, "resultPointId");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        return resultPointRepository.findAll(spec, pageable);
    }
}


